package com.dlc.apk.base

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners

object CustomBinds {
    @JvmStatic
    @BindingAdapter(value = ["android:imageUrl", "android:placeHolder", "android:error", "app:roundingRadius"], requireAll = false)
    fun loadImage(view: ImageView, url: String?, error: Drawable?, placeHolder: Drawable?, roundingRadius: Int? = 10) {
        Glide.with(view.context)
            .load(url)
            .transform(CenterCrop(), RoundedCorners(roundingRadius ?: 8))
            /*.centerCrop().circleCrop()*/
            .error(error)
            .placeholder(placeHolder)
            .into(view)
    }

    @JvmStatic
    @BindingAdapter("android:src")
    fun setSrc(view: ImageView, bitmap: Bitmap) {
        view.setImageBitmap(bitmap);
    }

    @JvmStatic
    @BindingAdapter("android:src")
    fun setSrc(view: ImageView, resId: Int) {
        view.setImageResource(resId);
    }

    @JvmStatic
    @BindingAdapter("app:background")
    fun setBackground(view: View, resId: Int) {
        view.background = view.context.resources.getDrawable(resId)
    }

    @JvmStatic
    @BindingAdapter("app:textColor")
    fun setTextColor(view: TextView, resId: Int) {
        view.setTextColor(resId)
    }
}